package com.baosight.risk.web.xtaa.sequences;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baosight.risk.api.xtaa.sequences.IXtaaSequencesService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.xtaa.sequences.entity.XtaaSequences;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 类 名: XtaaSequencesController
 * 描 述: 编码流水号管理
 * 作 者: 黎秋荣
 * 创 建：2019年04月15日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Slf4j
@RestController
@RequestMapping("/xtaa/sequences")
@Api(value = "编码流水号管理", description = "编码流水号管理", produces = MediaType.APPLICATION_JSON)
public class XtaaSequencesController extends BaseController {

    @Autowired
    private IXtaaSequencesService sequencesService;

    /**
     * 保存规则
     *
     * @param sequences
     * @return
     */
    @PostMapping("save")
    @Log(title = "保存规则")
    @ApiOperation("保存规则")
    public AjaxResult save(@RequestBody XtaaSequences sequences) {
        log.info("保存规则：" + sequences.toString());
        Integer row;
        if (StringUtils.isNotEmpty(sequences.getId())) {
            sequences = (XtaaSequences) BeanUtils.updateBean(sequences);
            row = sequencesService.update(sequences);
        } else {
            sequences = (XtaaSequences) BeanUtils.initBean(sequences);
            row = sequencesService.insertSequences(sequences);
        }
        return success(row);
    }

    /**
     * 根据ID查询跳转修改页面
     *
     * @param id
     * @return
     */
    @GetMapping("getById")
    @ApiOperation("规则添加")
    public AjaxResult edit(String id) {
        return success(sequencesService.get(id));
    }

    /**
     * 分页查询编码规则
     *
     * @param sequences
     * @return
     */
    @GetMapping("list")
    @ApiOperation("分页查询编码规则")
    public AjaxResult select(XtaaSequences sequences) {
        log.info("编码规则查询参数：" + sequences.toString());
        startPage();
        return getResultPage(sequencesService.selectSequences(sequences));
    }

    /**
     * 规则数据判断
     *
     * @param sequences
     * @return
     */
    @PostMapping("judge")
    public Boolean judge(@RequestBody XtaaSequences sequences) {
        log.info("编码规则判断是否重复参数：" + sequences.toString());
        Boolean flag = true;
        if (CollectionUtils.isEmpty(sequencesService.selectSequences(sequences))) {
            flag = false;
        }
        return flag;
    }


    /**
     * 批量逻辑删除编码规则
     *
     * @param updateStatus
     * @return
     */
    @PostMapping("delete")
    @Log(title = "规则删除", businessType = BusinessType.DELETE)
    @RequiresPermissions("xtaa:sequences:delete")
    public AjaxResult delete(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> coderuleList = sequencesService.getSequencesByIds(updateStatus.getIds());
        if (updateStatus.getIds().size() != coderuleList.size()) {
            log.error("数据有误！");
            return error("数据有误！");
        }
        return toAjax(sequencesService.deleteByIds(updateStatus.getIds(), updateStatus.getStatus()));
    }

    /**
     * 根据编号获取当前编码
     *
     * @param enCode
     * @return
     */
    @GetMapping("getByEnCode")
    @ApiOperation("根据编号获取当前编码")
    @Log(title = "根据编号获取当前编码", businessType = BusinessType.DELETE)
    public AjaxResult getByEnCode(@RequestParam("enCode") String enCode) {
        return success(sequencesService.getByEnCode(enCode));
    }

    /**
     * 保存修改后的当前编码
     *
     * @param enCode
     * @return
     */
    @PostMapping("saveCode")
    @ApiOperation("保存修改后的当前编码")
    @Log(title = "根据保存编码", businessType = BusinessType.DELETE)
    public AjaxResult saveCode(@RequestBody List<Map<String, String>> maps) {
        return success(sequencesService.saveCode(maps));
    }

    /**
     * 删除当前编码
     *
     * @param enCode
     * @return
     */
    @PostMapping("deleteCodes")
    @ApiOperation("删除缓存编码")
    @Log(title = "删除缓存", businessType = BusinessType.DELETE)
    public AjaxResult deleteCode(@RequestBody List<String> enCodes) {
        return success(sequencesService.deleteCodes(enCodes));
    }

    @GetMapping("selectRiaaByCode")
    @ApiOperation("查询编码")
    @Log(title = "查询", businessType = BusinessType.DELETE)
    public AjaxResult selectRiaaByCode(@RequestParam("enCode") String enCode) {
        return success(sequencesService.selectByCode("riaaRisk", enCode));
    }

    @GetMapping("selectRiriByCode")
    @ApiOperation("查询编码")
    @Log(title = "查询", businessType = BusinessType.DELETE)
    public AjaxResult selectRiriByCode(@RequestParam("enCode") String enCode) {
        List<Map<Object, Object>> ririRisk = sequencesService.selectByCode("ririRisk", enCode);
        return success(ririRisk);
    }
}

